package com.evan.review.architecture.mvvm.view;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.evan.review.R;

/**
 * 添加/编辑笔记Activity
 * MVVM架构中的View层的一部分
 */
public class AddEditNoteActivity extends AppCompatActivity {
    
    public static final String EXTRA_ID = "com.evan.review.architecture.mvvm.EXTRA_ID";
    public static final String EXTRA_TITLE = "com.evan.review.architecture.mvvm.EXTRA_TITLE";
    public static final String EXTRA_CONTENT = "com.evan.review.architecture.mvvm.EXTRA_CONTENT";
    public static final String EXTRA_CATEGORY = "com.evan.review.architecture.mvvm.EXTRA_CATEGORY";
    public static final String EXTRA_PRIORITY = "com.evan.review.architecture.mvvm.EXTRA_PRIORITY";
    
    private EditText editTextTitle;
    private EditText editTextContent;
    private Spinner spinnerCategory;
    private RadioGroup radioGroupPriority;
    private SeekBar seekBarPriority;
    private TextView textViewPriority;
    
    private boolean editMode = false;
    private int noteId = -1;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_edit_note);
        
        // 初始化视图
        initViews();
        
        // 设置分类下拉菜单
        setupCategorySpinner();
        
        // 设置优先级选择器
        setupPrioritySelector();
        
        // 处理传入的数据（编辑模式）
        handleIntent();
    }
    
    private void initViews() {
        editTextTitle = findViewById(R.id.edit_text_title);
        editTextContent = findViewById(R.id.edit_text_content);
        spinnerCategory = findViewById(R.id.spinner_category);
        radioGroupPriority = findViewById(R.id.radio_group_priority);
        seekBarPriority = findViewById(R.id.seek_bar_priority);
        textViewPriority = findViewById(R.id.text_view_priority);
        
        // 设置保存按钮点击事件
        findViewById(R.id.button_save).setOnClickListener(v -> saveNote());
    }
    
    private void setupCategorySpinner() {
        // 设置分类下拉菜单
        String[] categories = {"Android", "架构", "教程", "其他"};
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
                android.R.layout.simple_spinner_item, categories);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerCategory.setAdapter(adapter);
    }
    
    private void setupPrioritySelector() {
        // 设置优先级单选按钮组
        radioGroupPriority.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == R.id.radio_low) {
                seekBarPriority.setProgress(0); // 优先级1
            } else if (checkedId == R.id.radio_medium) {
                seekBarPriority.setProgress(2); // 优先级3
            } else if (checkedId == R.id.radio_high) {
                seekBarPriority.setProgress(4); // 优先级5
            }
        });
        
        // 设置优先级滑动条
        seekBarPriority.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                int priority = progress + 1; // 1-5
                textViewPriority.setText("当前优先级: " + priority);
                
                // 更新单选按钮状态
                if (priority <= 2) {
                    radioGroupPriority.check(R.id.radio_low);
                } else if (priority == 3) {
                    radioGroupPriority.check(R.id.radio_medium);
                } else {
                    radioGroupPriority.check(R.id.radio_high);
                }
            }
            
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }
            
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
    }
    
    private void handleIntent() {
        Intent intent = getIntent();
        
        if (intent.hasExtra(EXTRA_ID)) {
            // 编辑模式
            editMode = true;
            noteId = intent.getIntExtra(EXTRA_ID, -1);
            
            setTitle("编辑笔记");
            
            // 填充数据
            editTextTitle.setText(intent.getStringExtra(EXTRA_TITLE));
            editTextContent.setText(intent.getStringExtra(EXTRA_CONTENT));
            
            // 设置分类
            String category = intent.getStringExtra(EXTRA_CATEGORY);
            ArrayAdapter adapter = (ArrayAdapter) spinnerCategory.getAdapter();
            int position = adapter.getPosition(category);
            if (position >= 0) {
                spinnerCategory.setSelection(position);
            }
            
            // 设置优先级
            int priority = intent.getIntExtra(EXTRA_PRIORITY, 3);
            seekBarPriority.setProgress(priority - 1); // 转换为0-4
        } else {
            // 添加模式
            setTitle("添加笔记");
        }
    }
    
    private void saveNote() {
        String title = editTextTitle.getText().toString().trim();
        String content = editTextContent.getText().toString().trim();
        String category = spinnerCategory.getSelectedItem().toString();
        int priority = seekBarPriority.getProgress() + 1; // 1-5
        
        // 验证输入
        if (title.isEmpty() || content.isEmpty()) {
            Toast.makeText(this, "请填写标题和内容", Toast.LENGTH_SHORT).show();
            return;
        }
        
        // 创建返回数据
        Intent data = new Intent();
        data.putExtra(EXTRA_TITLE, title);
        data.putExtra(EXTRA_CONTENT, content);
        data.putExtra(EXTRA_CATEGORY, category);
        data.putExtra(EXTRA_PRIORITY, priority);
        
        // 如果是编辑模式，添加ID
        if (editMode && noteId != -1) {
            data.putExtra(EXTRA_ID, noteId);
        }
        
        setResult(RESULT_OK, data);
        finish();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_add_edit_note, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.save_note) {
            saveNote();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 